const Model = require('../../models/flexiPage/templatesModel')

const getAllTemplates = async (ctx) => {
  ctx.body = { success: true, data: await Model.getAllTemplates() }
}

const getTemplateById = async (ctx) => {
  const { id } = ctx.params
  const template = await Model.getTemplateById(id)
  if (!template) ctx.throw(404, 'Template not found')
  ctx.body = template
}

const createTemplate = async (ctx) => {
  const templateData = ctx.request.body
  const templateId = await Model.createTemplate(templateData)
  ctx.status = 201
  ctx.body = { template_id: templateId, ...templateData }
}

const updateTemplate = async (ctx) => {
  const { id } = ctx.params
  const templateData = ctx.request.body
  await Model.updateTemplate(id, templateData)
  ctx.body = { success: true }
}

const deleteTemplate = async (ctx) => {
  const { id } = ctx.params
  await Model.deleteTemplate(id)
  ctx.status = 204
}
module.exports = {
  getAllTemplates,
  getTemplateById,
  createTemplate,
  updateTemplate,
  deleteTemplate,
}
